package wjh.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import wjh.pojo.Patient;
import wjh.service.UserInfoService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.ParseException;

@Controller
@RequestMapping("/userInfo")
public class UserInfo {
    @Autowired
    private UserInfoService userInfoService;

    @RequestMapping("/parseId")
    public String parseId(@RequestParam("id") Integer id, HttpSession session){
        session.setAttribute("id",id);
        return "userIndex";
    }

    @RequestMapping("/findUserById")
    public String findUserById(@RequestParam("id")String id,Model model){
        Patient patient = userInfoService.findUserById(Integer.parseInt(id));
        model.addAttribute("patient",patient);
        model.addAttribute("id",Integer.parseInt(id));
        return "forward:/jsp/userInfo.jsp";
    }
    @RequestMapping("/updateInfo")
    public String updateInfo(Patient patient, MultipartFile file, HttpServletRequest request,Model model)throws ParseException {
        userInfoService.updateInfo(patient,file,request);
        int id=patient.getId();
        model.addAttribute("id",id);
        return "forward:/jsp/userIndex.jsp";//redirect:/user/findAll.do
    }
}
